<?php

namespace App\Http\Controllers\Auth;

use App\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;

class LoginController extends Controller
{

    use ApiResponse;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    public function bind(Request $request)
    {
        $phone = $request->phone;
        if ($phone) {

            /** @var \App\Models\User $user */
            $user = $request->user('api');
            $user->phone = $phone;
            $user->save();
            return $this->api("保存成功", 200,
                [
                    "username" => $user->username,
                    "password" => $user->password,
                    "nickname" => $user->nickname,
                    "phone" => $user->phone,
                ], "");
        } else {
            return $this->api('请传送正确的手机号', 400);
        }
    }

    public function auth(Request $request)
    {
        $username = $request->input("username", "");
//        dd($username);
        if (empty($username)) {
            return $this->api("账号不能为空", 400);
        }
        $login_url = env("LOGIN_URL");


        $password = $request->input("password", "");

        $user = User::whereUsername($username)->first();
        if (empty($user)) {
            $http = new Client();
            $url = $login_url;
            $response = $http->get($url, ['query' => ['username' => base64_encode($username), 'password' => base64_encode($password)]]);
            $result = json_decode((string)$response->getBody(), true);
            if ($result['success'] == false) {
                return $this->api("登录12319失败", 400);
            } else {
                $human = $result['data']['human'];
                $phone = $human['telMobile'];


                $insert['username'] = $username;
                $insert['password'] = bcrypt($password);
                $insert['nickname'] = "";
                $insert['phone'] = $phone;

                $user = User::create($insert);
                $access_token = \JWTAuth::fromUser($user);

                return $this->api("登录成功", 200,
                    [
                        "username" => $user->username,
                        "password" => $user->password,
                        "nickname" => $user->nickname,
                        "phone" => $user->phone,
                    ], $access_token);
//                return $this->api("登录12319成功", 200);
            }
        }
        if (\Hash::check($password, $user->password)) {
            $access_token = \JWTAuth::fromUser($user);

            return $this->api("登录成功", 200,
                [
                    "username" => $user->username,
                    "password" => $user->password,
                    "nickname" => $user->nickname,
                    "phone" => $user->phone,
                ], $access_token);
        } else {
            return $this->api("账号密码错误", 400);
        }
    }


}
